package com.loctek.importexport.mvp.acitvity;

import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loctek.importexport.R;
import com.loctek.importexport.base.BaseActivity;
import com.loctek.importexport.bean.getstoresiteResponse;
import com.loctek.importexport.bean.switchstoresiteResponse;
import com.loctek.importexport.mvp.adapter.ProductAdapter;
import com.loctek.importexport.mvp.presenter.TransferLibraryPersenter;
import com.loctek.importexport.mvp.view.TransferLibraryView;
import com.loctek.importexport.util.ToastUtils;
import com.loctek.importexport.views.UIConfirmTrabsferLibraryAlertView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * 移库
 */
public class TransferLibraryActivity extends BaseActivity<TransferLibraryView, TransferLibraryPersenter> implements TransferLibraryView {


    ProductAdapter newadapter;
    List<getstoresiteResponse.ProductInfo> newlist = new ArrayList<>();

    //    EditProductAdapter oldadapter;
    List<getstoresiteResponse.ProductInfo> oldlist = new ArrayList<>();
    @BindView(R.id.ed_storesite)
    EditText edStoresite;
    @BindView(R.id.iv_search_clear1)
    ImageView ivSearchClear1;
    @BindView(R.id.empty_ll)
    LinearLayout emptyLl;
    @BindView(R.id.llyt_old_layout)
    LinearLayout llytOldLayout;
    @BindView(R.id.item_ll)
    LinearLayout itemLl;
    @BindView(R.id.ed_storesite_new)
    EditText edStoresiteNew;
    @BindView(R.id.iv_search_clear_new)
    ImageView ivSearchClearNew;
    @BindView(R.id.empty_ll_new)
    LinearLayout emptyLlNew;
    @BindView(R.id.recyclerViewNew)
    RecyclerView recyclerViewNew;
    @BindView(R.id.item_ll_new)
    LinearLayout itemLlNew;
    @BindView(R.id.keji_loan_release_order_ll)
    LinearLayout kejiLoanReleaseOrderLl;

    getstoresiteResponse newData;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_transfer_library;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        setTitle(getResources().getString(R.string.whole_support_transfer));


        newadapter = new ProductAdapter(TransferLibraryActivity.this, newlist);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(TransferLibraryActivity.this);
        recyclerViewNew.setLayoutManager(linearLayoutManager);
        recyclerViewNew.setItemAnimator(new DefaultItemAnimator());
        recyclerViewNew.setAdapter(newadapter);


        edStoresite.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    ivSearchClear1.setVisibility(View.VISIBLE);
                    emptyLl.setVisibility(View.GONE);
                    itemLl.setVisibility(View.GONE);
                    oldlist.clear();
                    llytOldLayout.removeAllViews();
                    getPresenter().getoldstoresite(editable.toString());
                } else {
                    ivSearchClear1.setVisibility(View.GONE);
                    emptyLl.setVisibility(View.GONE);
                    itemLl.setVisibility(View.GONE);
                    oldlist.clear();
                    llytOldLayout.removeAllViews();
                }
            }
        });

        ivSearchClear1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edStoresite.setText("");
            }
        });


        edStoresiteNew.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())) {
                    ivSearchClearNew.setVisibility(View.VISIBLE);
                    emptyLlNew.setVisibility(View.GONE);
                    itemLlNew.setVisibility(View.GONE);
                    newlist.clear();
                    newadapter.notifyDataSetChanged();
                    getPresenter().getnewstoresite(editable.toString());
                } else {
                    ivSearchClearNew.setVisibility(View.GONE);
                    emptyLlNew.setVisibility(View.GONE);
                    itemLlNew.setVisibility(View.GONE);
                    newlist.clear();
                    newadapter.notifyDataSetChanged();
                }
            }
        });

        ivSearchClearNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                edStoresiteNew.setText("");
            }
        });


        edStoresite.setFocusable(true);
        edStoresite.setFocusableInTouchMode(true);
        edStoresite.requestFocus();
        kejiLoanReleaseOrderLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CheckParam()) {

                    final getstoresiteResponse oldData = new getstoresiteResponse();
                    oldData.data = new getstoresiteResponse.StoresiteInfo();
                    oldData.data.productList = new ArrayList<>();

                    for (int i = 0; i < llytOldLayout.getChildCount(); i++) {
                        View rootView = llytOldLayout.getChildAt(i);
                        TextView tv_productname = (TextView) rootView.findViewById(R.id.tv_productname);
                        TextView tv_scdptypename = (TextView) rootView.findViewById(R.id.tv_scdptypename);
                        EditText edit_num = (EditText) rootView.findViewById(R.id.edit_num);
                        CheckBox cb_sel = (CheckBox) rootView.findViewById(R.id.cb_sel);
                        if (cb_sel.isChecked()) {
                            if (TextUtils.isEmpty(edit_num.getText().toString().trim())) {
                                ToastUtils.showToast(tv_productname.getText() + getResources().getString(R.string.data_more_than_zerro));
                                return;
                            } else {
                                int num = 0;
                                try {
                                    num = Integer.parseInt(edit_num.getText().toString().trim());
                                    if (num < 1) {
                                        ToastUtils.showToast(tv_productname.getText() + getResources().getString(R.string.data_more_than_zerro));
                                        return;
                                    }
                                } catch (Exception e) {
                                    ToastUtils.showToast(tv_productname.getText() + getResources().getString(R.string.correct_number_format));
                                    return;
                                }
                                getstoresiteResponse.ProductInfo info = new getstoresiteResponse.ProductInfo();
                                info.productname = tv_productname.getText().toString();
                                info.scdptypename = tv_scdptypename.getText().toString();
                                info.num = num;
                                oldData.data.productList.add(info);
                            }

                        }
                    }

                    if (oldData.data.productList.size() == 0) {
                        ToastUtils.showToast(getResources().getString(R.string.least_one_piece_of_goods));
                        return;
                    }

                    final UIConfirmTrabsferLibraryAlertView alertView = new UIConfirmTrabsferLibraryAlertView(TransferLibraryActivity.this, oldData, edStoresite.getText().toString(), TransferLibraryActivity.this.newData, edStoresiteNew.getText().toString());
                    alertView.setClicklistener(new UIConfirmTrabsferLibraryAlertView.ClickListenerInterface() {
                        @Override
                        public void doLeft() {
                            alertView.dismiss();
                        }

                        @Override
                        public void doRight() {

//                            oldData
                            List<PostItem> post = new ArrayList<>();
                            for (int i = 0; i < oldData.data.productList.size(); i++) {
                                PostItem item = new PostItem();
                                item.productname = oldData.data.productList.get(i).productname;
                                item.num = oldData.data.productList.get(i).num;
                                post.add(item);
                            }

                            getPresenter().switchstoresite(edStoresite.getText().toString(), edStoresiteNew.getText().toString(), new Gson().toJson(post));
                            alertView.dismiss();
                        }
                    });
                    alertView.show();
                }
            }
        });

    }

    public static class PostItem {
        public String productname;
        public int num;
    }

    private boolean CheckParam() {
        if (TextUtils.isEmpty(edStoresite.getText().toString())) {
            ToastUtils.showToast(getResources().getString(R.string.sweep_old_library_bit_code));
            return false;
        }


        if (TextUtils.isEmpty(edStoresiteNew.getText().toString())) {
            ToastUtils.showToast(getResources().getString(R.string.sweep_new_library_bit_code));
            return false;
        }
        if (llytOldLayout.getChildCount() == 0) {
            ToastUtils.showToast(getResources().getString(R.string.least_one_piece_of_goods));
            return false;
        }
        if (newData == null) {
            ToastUtils.showToast(getResources().getString(R.string.sweep_new_library_bit_code));
            return false;
        }


        return true;
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }


    @NonNull
    @Override
    public TransferLibraryPersenter createPresenter() {
        return new TransferLibraryPersenter(getApp());
    }


    @Override
    public void getnewstoresite(getstoresiteResponse data) {
        newData = data;
        if (data.data.productList == null || data.data.productList.size() == 0) {
            emptyLlNew.setVisibility(View.VISIBLE);
            itemLlNew.setVisibility(View.GONE);
        } else {
            emptyLlNew.setVisibility(View.GONE);
            itemLlNew.setVisibility(View.VISIBLE);
            newlist.addAll(data.data.productList);
            newadapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getoldstoresite(getstoresiteResponse data) {
        edStoresiteNew.setFocusable(true);
        edStoresiteNew.setFocusableInTouchMode(true);
        edStoresiteNew.requestFocus();

        if (data.data.productList == null || data.data.productList.size() == 0) {
            emptyLl.setVisibility(View.VISIBLE);
            itemLl.setVisibility(View.GONE);
        } else {
            emptyLl.setVisibility(View.GONE);
            itemLl.setVisibility(View.VISIBLE);
            oldlist.addAll(data.data.productList);

            for (int i = 0; i < data.data.productList.size(); i++) {
                View rootView = LayoutInflater.from(TransferLibraryActivity.this).inflate(R.layout.cell_eidt_product_item, null);
                TextView tv_productname = (TextView) rootView.findViewById(R.id.tv_productname);
                TextView tv_scdptypename = (TextView) rootView.findViewById(R.id.tv_scdptypename);
                EditText edit_num = (EditText) rootView.findViewById(R.id.edit_num);
                tv_productname.setText(data.data.productList.get(i).productname + "");
                tv_scdptypename.setText(data.data.productList.get(i).scdptypename + "");
                edit_num.setHint("0 ~ " + data.data.productList.get(i).num + "");
                edit_num.setText(data.data.productList.get(i).num + "");

                llytOldLayout.addView(rootView);
            }
        }
    }

    @Override
    public void switchstoresite(switchstoresiteResponse data) {
        oldlist.clear();
        llytOldLayout.removeAllViews();
        getPresenter().getoldstoresite(edStoresite.getText().toString());


        newlist.clear();
        newadapter.notifyDataSetChanged();
        getPresenter().getnewstoresite(edStoresiteNew.getText().toString());

        ToastUtils.showToast(getResources().getString(R.string.move_library_success));
    }

}
